﻿<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <!--[if lt IE 9]>
    <script type="text/javascript" src="lib/html5shiv.js"></script>
    <script type="text/javascript" src="lib/respond.min.js"></script>
    <![endif]-->
    <link href="static/h-ui/css/H-ui.min.css" rel="stylesheet" type="text/css"/>
    <link href="static/h-ui.admin/css/H-ui.login.css" rel="stylesheet" type="text/css"/>
    <link href="static/h-ui.admin/css/style.css" rel="stylesheet" type="text/css"/>
    <link href="lib/Hui-iconfont/1.0.8/iconfont.css" rel="stylesheet" type="text/css"/>
    <!--[if IE 6]>
    <script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js"></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <title>后台登录 - H-ui.admin v3.0</title>
    <meta name="keywords" content="H-ui.admin v3.0,H-ui网站后台模版,后台模版下载,后台管理系统模版,HTML后台模版下载">
    <meta name="description"
          content="H-ui.admin v3.0，是一款由国人开发的轻量级扁平化网站后台模板，完全免费开源的网站后台管理系统模版，适合中小型CMS后台系统。">
</head>
<body>
<input type="hidden" id="TenantId" name="TenantId" value=""/>
<div class="header"></div>
<div class="loginWraper" id="app">
    <div id="loginform" class="loginBox">
        <h5 style="text-align: center;margin: 0px auto;padding: 0px;color:cornflowerblue;font-weight: bolder;">
            用户注册</h5>
        <form class="form form-horizontal">
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60d;</i></label>
                <div class="formControls col-xs-8">
                    <input id="identify" name="identify" v-model="regUser.identify" type="text"
                           placeholder="请输入注册手机号码/电子邮箱"
                           class="input-text size-L">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-3"><i class="Hui-iconfont">&#xe60e;</i></label>
                <div class="formControls col-xs-8">
                    <input id="password" name="password" v-model="regUser.password" type="password"
                           placeholder="请输入密码"
                           class="input-text size-L">
                    <br>
                    <input id="confirmpass" name="password" v-model="regUser.confirmPass" type="password"
                           placeholder="请输入确认密码"
                           class="input-text size-L">
                </div>
            </div>
            <div class="row cl">
                <div class="formControls col-xs-8 col-xs-offset-3">
                    <input class="input-text size-L" type="text" id="randomCode" placeholder="请输入验证码"
                           value="" v-model="regUser.validateCode" style="width:120px;">
                    <input name="" type="button" class="btn btn-default radius size-L"
                           value="发送验证码" @click="sendMsg">
                </div>
            </div>

            <div class="row cl">
                <div class="formControls col-xs-8 col-xs-offset-3">
                    <input name="" type="button" class="btn btn-primary radius size-L"
                           value="注册" @click="doReg">
                    <input name="" type="reset" class="btn btn-default radius size-L"
                           value="取消">
                    &nbsp;&nbsp;<a href="login.html">立即登录</a>
                </div>
            </div>

        </form>
    </div>
</div>
<div class="footer"> &copy; 华清远见 2017-2023 &nbsp;&nbsp; 作者: 西蒙牛 v1.0</div>
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>
<!--此乃百度统计代码，请自行删除-->
<!--引入公共的组件库，减少屎山代码-->
<script src="js/commons/commonlibs.js"></script>


<script type="module">
    import {nanoid} from "./js/util/nanoid.js";

    let vm = new Vue({
        el: '#app',
        data: {
            token: nanoid(32),
            //要注册的用户对象
            regUser: {
                identify: '', //这个标识符有可能是手机，也有可能是邮箱。
                mobile: '',
                email: '',
                password: '',
                confirmPass: '',
                validateCode: ''
            }
        },
        created() {

        },
        mounted() {
            console.log('this page is mounted....')
            console.log("this page token is:" + this.token)
        },
        methods: {
            async regUserIdentifyValidate() {
                if (outils.isPhoneNum(this.regUser.identify)) {
                    console.log("您注册使用的是手机号码...")
                    this.regUser.mobile = this.regUser.identify;
                    return true;
                } else if (outils.isEmail(this.regUser.identify)) {
                    console.log("您注册使用的是电子邮箱...")
                    this.regUser.email = this.regUser.identify;
                    return true;
                } else {
                    console.log("您注册的数据格式非法.....");
                    this.$message({
                        type: 'error',
                        message: '注册数据格式非法！'
                    })
                    return false; //表单验证失败....
                }
            },

            async regUserFormValidate() {
                if (await this.regUserIdentifyValidate()) {
                    //检查密码不能少于6位
                    if (this.regUser.password.length < 6) {
                        this.$message({
                            type: 'error',
                            message: '密码不能少于6位'
                        })
                        return false;
                    } else if (this.regUser.password !== this.regUser.confirmPass) {
                        //如果密码和确认密码不相等。
                        this.$message({
                            type: 'error',
                            message: '密码与确认密码不一致！'
                        })
                        return false;
                    }
                    return true;

                }
                return false;

            },

            async sendMsg() {
                console.log('发送短信验证码....')
                if (await this.regUserIdentifyValidate()) {
                    let params = {
                        identify: this.regUser.identify,
                        token: this.token
                    };
                    sendRadomCodeMsg(params).then(resp => {
                        if (resp.data.code === 200) {
                            this.$message({
                                type: 'success',
                                message: '短信验证码发生成功！'
                            })
                        }
                    })
                }
            },

            //检查验证码是否正确。
            async checkValiateCodeIsCorrect() {
                let params = {
                    token: this.token,
                    code: this.regUser.validateCode
                }
                let flag = await validateRandomCode(params).then(resp => {
                    if (resp.data.code === 200) {
                        return true;
                    } else {
                        return false;
                    }
                })

                return flag;
            },

            async doReg() {

                //第一步进行表单验证，验证你注册信息是否合法
                let flag = await this.regUserFormValidate();
                if (flag) { //您已经通过了表单验证，下来咱们就执行真正的用户注册。。。。
                    console.log("执行用户注册....")
                    console.log("----------要注册的用户资料是-----------");
                    console.log(this.regUser);

                    //检查验证码是否通过
                    if (await this.checkValiateCodeIsCorrect()) {
                        console.log('短信验证码验证通过....')

                        reg(this.regUser).then(resp => {
                            if (resp.data.code === 200) {
                                this.$message({
                                    type: 'success',
                                    message: resp.data.msg
                                })
                            }

                            this.regUser.identify = "";
                            this.regUser.password = "";
                            this.regUser.confirmPass = "";
                            this.regUser.validateCode = "";

                            setTimeout(()=>{
                                window.location.href= 'login.html';
                            },1000);
                        })
                    } else {
                        console.log('短信验证码验证失败.....');
                        this.$message({
                            type: 'error',
                            message : '验证码验证失败！'
                        })
                    }

                }
            }
        }

    })
</script>
<!--/此乃百度统计代码，请自行删除
</body>
</html>